package com.mgface.completableFuture;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class Test {
	static class Person{
		private int id;
		private String name;

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return "Person [id=" + id + ", name=" + name + "]";
		}

		public Person(int id, String name) {
			this.id = id;
			this.name = name;
		}

	}

	public <T> T get(String str) {
		return (T) str;
	}

	public static void main(String[] args) {
		Test t = new Test();
		t.<String> get("123");
		Person p1 = new Person(1000, "mazi"); 
		Person p2 = new Person(1002, "wangwu");
		Person p3 = new Person(1002, "");
		Person p5 = new Person(1002, "lala");
		Person p4 = new Person(1004, "zhangshan");
		Map<Integer, Long> map = Stream.of(p1,p2,p3,p4).collect(Collectors.groupingBy(Person::getId,Collectors.counting()));
		Map<Integer, Integer> map1 = Stream.of(p1,p2,p3,p4).collect(Collectors.groupingBy(Person::getId,Collectors.summingInt(Person::getId)));
		Map<Integer, Optional<Person>>  map2 = Stream.of(p1,p2,p3,p4).collect(Collectors.groupingBy(Person::getId,Collectors.maxBy(Comparator.comparing(Person::getName))));
		//map2.forEach((x,y)->System.out.println(x+"|"+y.get()));
		Map<Integer, Optional<String>> map4 = Stream.of(p1,p2,p3,p4).collect(Collectors.groupingBy(Person::getId,Collectors.mapping(Person::getName, Collectors.minBy(Comparator.comparing(String::length)))));
		Map<Integer, ArrayList<String>> map5 =  Stream.of(p1,p2,p3,p4).collect(Collectors.groupingBy(Person::getId,Collectors.mapping(Person::getName, Collectors.toCollection(ArrayList::new))));
		Map<Integer, Map<String, List<Person>>> map6 =  Stream.of(p1,p2,p3,p4).collect(Collectors.groupingBy(Person::getId,Collectors.groupingBy(Person::getName)));
		Map<Integer, IntSummaryStatistics> map7 = Stream.of(p1,p2,p3,p4).collect(Collectors.groupingBy(Person::getId,Collectors.summarizingInt(Person::getId)));
		Map<Integer, String> map8 = Stream.of(p1,p2,p3,p4,p5).collect(Collectors.groupingBy(Person::getId,Collectors.reducing("", Person::getName, (x,y)->x.length()==0?y:x+","+y)));
		//System.out.println(IntStream.range(0, 10).summaryStatistics());
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		Stream<String> stream = list.stream();
		list.add("4");
		list.forEach(System.out::println);
		//System.out.println(map8);
		//System.out.println(map);
		//System.out.println(map1);
		//System.out.println(map2);
		//System.out.println(map4);
	}

}
